﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using OConf.API.Filter;
using OConf.IBLL;
using OConf.Model;
using OConf.Model.Common;
using OConf.Model.RequestInput.Users;
using System;
using System.Collections.Generic;
using System.Linq;

namespace OConf.API.Controllers
{
    [Authorize]
    [Route("api/[Controller]/[Action]")]
    [ApiController]
    public class AdminController : ControllerBase
    {
        private readonly IAdminBLL _adminBLL;

        public AdminController(IAdminBLL adminBLL)
        {
            _adminBLL = adminBLL;
        }

        #region 用户操作
        /// <summary>
        /// 获取个人中心信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ReturnData<UserPersonalCenterModel> GetPersonalInfo()
        {
            return _adminBLL.GetPersonalInfo();
        }

        /// <summary>
        /// 个人中心修改信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public ReturnData<bool> ModifyUserSelfInfo([FromBody] EditPersionalInfoInput input)
        {
            return _adminBLL.ModifyUserSelfInfo(input);
        }

        /// <summary>
        /// 个人中心修改密码
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public ReturnData<bool> ModifyUserPassWord([FromBody] EditPersionalPwdInput input)
        {
            return _adminBLL.ModifyUserPassWord(input);
        }

        #endregion 用户操作

        /// <summary>
        /// 管理后台用户分页查询
        /// </summary>
        /// <param name="queryParam"></param>
        /// <returns></returns>
        [HttpGet]
        [PermissionForApi(AllowRole = Entity.RoleTypeEnum.超级管理员)]
        public ReturnData<ReturnTableData<UsersModel>> QueryPage([FromQuery] QueryUserInput queryParam)
        {
            return _adminBLL.QueryPage(queryParam);
        }

        /// <summary>
        /// 获取用户角色下拉列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ReturnData<List<DropDownListModel<int>>> GetRoleTypeDropDownList()
        {
            return _adminBLL.GetRoleTypeDropDownList();
        }

        /// <summary>
        /// 用户注册
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [PermissionForApi(AllowRole = Entity.RoleTypeEnum.超级管理员)]
        public ReturnData<bool> RegisterUser([FromBody] RegisterUserInput input)
        {
            if (!new int[] { 2, 3 }.Contains(input.RoleType))
            {
                return ReturnDataHelper<bool>.Success("用户角色不存在", false);
            }

            return _adminBLL.RegisterUser(input);
        }

        /// <summary>
        /// 修改管理员状态
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [PermissionForApi(AllowRole = Entity.RoleTypeEnum.超级管理员)]
        public ReturnData<bool> ModifyUserEnable([FromBody] ModifyUserEnableInput input)
        {
            return _adminBLL.ModifyUserEnable(input);
        }

        /// <summary>
        /// 修改管理员信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [PermissionForApi(AllowRole = Entity.RoleTypeEnum.超级管理员)]
        public ReturnData<bool> EditUser([FromBody] EditUserInput input)
        {
            if (!new int[] { 2, 3 }.Contains(input.RoleType))
            {
                return ReturnDataHelper<bool>.Success("用户角色不存在", false);
            }

            return _adminBLL.EditUser(input);
        }

        /// <summary>
        /// 删除管理员信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [PermissionForApi(AllowRole = Entity.RoleTypeEnum.超级管理员)]
        public ReturnData<bool> DeleteUser([FromBody] DeleteUserInput input)
        {
            return _adminBLL.DeleteUser(input);
        }

        /// <summary>
        /// 获取管理员详情
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet]
        [PermissionForApi(AllowRole = Entity.RoleTypeEnum.超级管理员)]
        public ReturnData<UserDetailModel> GetUserDetail([FromQuery] GetUserDetailInput input)
        {
            return _adminBLL.GetUserDetail(input.ID);
        }

        /// <summary>
        /// 获取管理后台菜单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ReturnData<List<AdminBetterRouterModel>> GetAdminBetterManageMenu()
        {
            return _adminBLL.GetAdminBetterManageMenu();
        }
    }
}